<?php

//	$url = "http://ogc.gbif.org/wms?REQUEST=GetFeatureInfo&EXCEPTIONS=application%2Fvnd.ogc.se_xml&BBOX=15.652005%2C-2.732409%2C16.931505%2C-1.562866&X=307&Y=235&INFO_FORMAT=application/vnd.ogc.gml&QUERY_LAYERS=gbif%3Aafrican_mammals&FEATURE_COUNT=50&Srs=EPSG%3A4326&Layers=gbif%3Aafrican_mammals&Styles=&WIDTH=512&HEIGHT=468&format=image%2Fpng";
	$url = $_REQUEST['url'];
	
	$expected=array(
			'version'
		,	'srs'
		,	'format'
		,	'bbox'
		,	'width'
		,	'height'
		,	'styles'
		,	'query_layers'
		,	'x'
		,	'y'
		,	'info_format'
		,	'feature_count'
		,	'exceptions'
		,	'servicename'
		,	'request'
		,	'layers'
		,	'typename'
		,	'cql_filter'
		,	'filter'
	);
	
	// Initialize allowed variables
	foreach ($expected as $formvar)
		$$formvar = (isset(${"_$_SERVER[REQUEST_METHOD]"}[$formvar])) ? ${"_$_SERVER[REQUEST_METHOD]"}[$formvar]:NULL;	

	$extra = "?";
	foreach($expected as $name){
		if ($$name != '') {
			$extra .= strtoupper($name) . "=" . urlencode(stripslashes($$name)) . "&";
		}
	}

#print $url . $extra;

	require_once("xml2json/xml2json.php");
	$xmlStringContents = file_get_contents($url . $extra); 
	$list = array("gbif:", "gml:", "geobon:", "birdlife:", "xsd:", "groms:");
	$xmlStringContents = str_replace( $list, "", $xmlStringContents);

//print $xmlStringContents;	
	$json = xml2json::transformXmlStringToJson($xmlStringContents);
	$json = str_replace("@attributes", "attributes", $json);
	header('Content-type: application/json');
	print $json;

?>